home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Mac Game Programming Gurus / TricksOfTheMacGameProgrammingGurus.iso / More Source / Libraries / VideoToolbox 95.04.18 / Demos / Grating.c < prev    next >
C/C++ Source or Header  |  1995-04-12  |  8KB  |  212 lines

  1. /*
  2. Grating.c
  3. This demo shows how to load the clut and put a vignetted grating onto the
  4. screen. It also saves the image to disk as a PICT file. This demo has been kept
  5. as simple as possible, to enhance readability, forsaking accurate control of
  6. time and contrast of the stimulus. The FlickeringGrating demo, on the other
  7. hand, is somewhat more elaborate and presents a research-grade stimulus if the
  8. LuminanceRecord.h calibration data are accurate.
  9.  
  10. For a real experiment there are some refinements you should consider. This demo
  11. takes a while to create the image on the screen, but you probably want
  12. frame-accurate timing in your experiment. You could either create the image in
  13. an offscreen GWorld and copy it to the screen with CopyBitsQuickly. Or you could
  14. set the clut to uniform gray (every clut entry equal to the background
  15. luminance) while you're computing the image on-screen, and then load a grayscale
  16. ramp into the clut when you want the display to start. Similarly you can make
  17. the image disappear by loading a new image, calling EraseRect(), or loading 
  18. a uniform-gray clut.
  19.  
  20. Another issue that matters for a real experiment is gamma correction. The
  21. numbers loaded into the clut are faithfully transformed into voltages by the
  22. three digital-to-analog converters on your video card, but the resulting
  23. luminance produced on your monitor will be an approximately parabolic function
  24. of the video voltage. Apple provides crude gamma correction by means of a
  25. generic 8-bit gamma table in the video driver, which does make things look
  26. better, but is not accurately matched to the gamma of your particular monitor,
  27. which depends on the current settings of its brightness and contrast knobs.
  28. Furthermore the Apple 8-bit gamma-correction scheme, which simply transforms the
  29. nominal 8-bit clut value into a new 8-bit clut value, necessarily restricts the
  30. range of available values, mapping several onto one output value, and omitting
  31. some output values. In other words your luminances will be 8-bit quantized
  32. twice, both before and after gamma correction. It is preferable to do gamma
  33. correction first, without quantization. Therefore I suggest you eliminate
  34. Apple's gamma correction, by calling GDUncorrectedGamma(), and use the
  35. Luminance.c package to do gamma correction. That package implements the
  36. published algorithm of Pelli and Zhang (1991). The FlickeringGrating demo uses
  37. Luminance.c.
  38.  
  39. Why not merge main() and Grating()? The call to Require() checks for any needed
  40. hardware. It is vital that a test for the presence of a needed floating point
  41. unit be done before entering any routine, e.g. Grating(), that uses the fpu,
  42. because the THINK C compiler typically produces code that accesses the fpu, to
  43. save fpu registers, at the beginning of the routine, so any subsequent check
  44. would be too late.
  45.  
  46. HISTORY:
  47. 2/7/93    dgp wrote it, as an answer to questions from Bill Merigan and David Brainard.
  48. 2/18/93    dgp    added fpu test.
  49. 2/23/93    dgp    use new GDOpenWindow1 and GDDisposeWindow1.
  50. 4/18/93    dgp    support directType.
  51. 7/7/93    dgp    prefer screen 1. Added ScrollRect for compatibility with Radius PowerView.
  52. 10/2/93    dgp    added SAVE_PICT to test PixMapToPICT().
  53. 4/27/94 dgp now ask user whether to save grating as PICT.
  54. 8/11/94    dgp don't redefine "sin".
  55. 9/5/94 dgp removed assumption in printf's that int==short.
  56. 3/20/95 dgp removed SelectWindow() workaround for CW5 SIOUX bug.
  57. 4/11/95 dgp set and restore display depth and color. Use ChooseScreen().
  58. */
  59. #include "VideoToolbox.h"
  60. #include <math.h>
  61. #if (THINK_C || THINK_CPLUS)
  62.     #include <console.h>
  63. #endif
  64. #if __MWERKS__
  65.     #include <SIOUX.h>
  66. #endif
  67. #if UNIVERSAL_HEADERS
  68.     #include <LowMem.h>
  69. #else
  70.     #define LMGetMBarHeight() (* (short *) 0x0BAA)
  71.     #define LMSetMBarHeight(MBarHeightValue) ((* (short *) 0x0BAA) = (MBarHeightValue))
  72. #endif
  73. #include "mc68881.h"
  74. #if mc68881 && THINK_C    // use explicit fpu instructions for highest possible speed
  75.     #undef exp
  76.     #define exp _exp
  77. #endif
  78. void Grating(void);
  79. #define SIZE 300
  80. #define POWERVIEW 0
  81.  
  82. void main(void)
  83. {
  84.     Require(gestalt8BitQD);
  85.     Grating();
  86. }
  87.  
  88. void Grating(void)
  89. {
  90.     short i,j,error,clutSize,pixelSize,oldPixelSize,oldIsColor,mode;
  91.     short preferredPixelSize[6]={8,32,16,4,2,1};    // Order of preference
  92.     GDHandle device;
  93.     static ColorSpec table[256];
  94.     WindowPtr window,oldPort;
  95.     Rect r;
  96.     double a,fX[SIZE],fY[SIZE];
  97.     char string[100];
  98.     Point pt;
  99.     Boolean savePict=1,saveEPS=1;
  100.     
  101.     StackGrow(10000);
  102.     #if (THINK_C || THINK_CPLUS)
  103.         console_options.top = 0;
  104.         console_options.left = 0;
  105.         console_options.nrows = 4;
  106.         console_options.ncols = 60;
  107.         printf("\n");
  108.     #elif __MWERKS__
  109.         SIOUXSettings.toppixel=LMGetMBarHeight()+1;    // allow for menu bar only
  110.         SIOUXSettings.leftpixel=1;
  111.         SIOUXSettings.rows=4;
  112.         SIOUXSettings.columns=60;
  113.         SIOUXSettings.autocloseonquit=0;
  114.         SIOUXSettings.showstatusline=0;
  115.         SIOUXSettings.asktosaveonclose=0;
  116.         printf("\n");
  117.     #else
  118.         InitGraf(&qd.thePort);
  119.         InitFonts();
  120.         InitWindows();
  121.         InitCursor();
  122.     #endif
  123.     printf("Welcome to Grating.\n");
  124.     GetPort(&oldPort);
  125.  
  126.     /* Find device corresponding to the experimental screen. */
  127.     for(i=8;i>=0;i--){
  128.         device = GetScreenDevice(i);
  129.         if(device!=NULL) break;
  130.     }
  131.     do{
  132.         if(GetScreenDevice(1)!=NULL)i=ChooseScreen(i,"Which screen?");
  133.         else i=0;
  134.         device=GetScreenDevice(i);
  135.     }while(device==NULL);
  136.     
  137.     // Try to get the best possible pixelSize.
  138.     oldPixelSize=(**(**device).gdPMap).pixelSize;
  139.     oldIsColor=TestDeviceAttribute(device,gdDevType);
  140.     if(oldPixelSize!=preferredPixelSize[0] && NewPaletteManager()){
  141.         for(i=0;i<sizeof(preferredPixelSize)/sizeof(*preferredPixelSize);i++){
  142.             if(oldPixelSize==preferredPixelSize[i] && oldIsColor)break;
  143.             mode=HasDepth(device,preferredPixelSize[i],0,0);
  144.             if(mode!=0){
  145.                 printf("Changing pixelSize to %d bits, color.\n",(int)preferredPixelSize[i]);
  146.                 error=SetDepth(device,preferredPixelSize[i],1<<gdDevType,1);    // color
  147.                 break;
  148.             }
  149.         }
  150.     }
  151.     pixelSize=(**(**device).gdPMap).pixelSize;
  152.  
  153.     savePict=Choose(savePict,"Save grating to disk as a PICT file?\n",noYes,2);
  154.     saveEPS=Choose(saveEPS,"Save grating to disk as an EPS file?\n",noYes,2);
  155.     
  156.     // Load the clut with a grayscale ramp.
  157.     GDSaveGamma(device);
  158.     GDUncorrectedGamma(device);    // Tell the driver to faithfully copy our colors into 
  159.                                 // the clut without any transformation.
  160.     clutSize=GDClutSize(device);
  161.     for(i=0;i<clutSize;i++){
  162.         table[i].rgb.red=table[i].rgb.green=table[i].rgb.blue=i*(long)0xffff/(clutSize-1);
  163.     }
  164.     error=GDSetEntriesByType(device,0,clutSize-1,table);
  165.  
  166.     // Open window and put grating in it.
  167.     window=GDOpenWindow1(device);
  168.     SetPort(window);
  169.     PmBackColor((clutSize-1)/2);    // This works because GDOpenWindow1 marked
  170.     EraseRect(&window->portRect);    // all the colors as pmExplicit.
  171.     SetRect(&r,0,0,SIZE,SIZE);
  172.     CenterRectInRect(&r,&window->portRect);
  173.     for(i=0;i<SIZE;i++){
  174.         a=(i-SIZE/2)/(SIZE/6.);
  175.         fY[i]=exp(-a*a);
  176.         fX[i]=fY[i]*sin((i-SIZE/2)*(2.0*PI/80.0));
  177.     }
  178.     pt.h=pt.v=0;
  179.     LocalToGlobal(&pt);
  180.     ShieldCursor(&r,pt);
  181.     for(j=0;j<SIZE;j++){
  182.         unsigned long row[SIZE];
  183.         for(i=0;i<SIZE;i++) row[i]=0.5+(clutSize-1)*0.5*(1.0+fY[j]*fX[i]);
  184.         if(pixelSize==16)for(i=0;i<SIZE;i++) row[i]*=1+(1<<5)+(1<<10);
  185.         if(pixelSize==32)for(i=0;i<SIZE;i++) row[i]*=1+(1<<8)+(1UL<<16);
  186.         SetPixelsQuickly(r.left,r.top+j,row,SIZE);
  187.     }
  188.     ShowCursor();
  189.     if(savePict){
  190.         PixMapToPICT("grating.pict",((CWindowPtr)window)->portPixMap
  191.             ,&r,2,NULL);
  192.         printf("Image was saved to disk as file “grating.pict”.\n");
  193.     }
  194.     if(saveEPS){
  195.         WindowToEPS((CWindowPtr)window,"grating.eps",&r,&r,0,0,NULL);
  196.         printf("Image was saved to disk as file “grating.eps.\n");
  197.     }
  198.     SetPort((WindowPtr)oldPort);
  199.     printf("Done. Hit return to quit.\n");
  200.     gets(string);
  201.     GDDisposeWindow1(window);
  202.     GDRestoreGamma(device);
  203.     GDRestoreDeviceClut(device);
  204.     // restore
  205.     error=SetDepth(device,oldPixelSize,1<<gdDevType,oldIsColor);
  206.     #if (THINK_C || THINK_CPLUS)
  207.         abort();
  208.     #elif __MWERKS__
  209.         SIOUXSettings.autocloseonquit=1;
  210.     #endif
  211. }
  212.